# !pip install scipy
import numpy as np
import matplotlib.pyplot as plt
from scipy import misc # contains an image of a racoon!
from PIL import Image # for reading image files
my_array = np.array([1.1, 9.2, 8.1, 4.7])
my_array.shape
(4,)
my_array.ndim
1
array_2d = np.array([[1, 2, 3, 9],
[5, 6, 7, 8]])
array_2d[0, :]
array([1, 2, 3, 9])
array_2d[:, 0]
array([1, 5])
print(f'array_2d has {array_2d.ndim} dimensions')
print(f'Its shape is {array_2d.shape}')
print(f'It has {array_2d.shape[0]} rows and {array_2d.shape[1]} columns')
print(array_2d)
array_2d has 2 dimensions Its shape is (2, 4) It has 2 rows and 4 columns [[1 2 3 9] [5 6 7 8]]
Challenge:
18 in the last line of code.[97, 0, 27, 18][[ 0, 4], [ 7, 5], [ 5, 97]]Hint: You can use the : operator just as with Python Lists.
mystery_array = np.array([[[0, 1, 2, 3],
[4, 5, 6, 7]],
[[7, 86, 6, 98],
[5, 1, 0, 4]],
[[5, 36, 32, 48],
[97, 0, 27, 18]]])
# Note all the square brackets!
mystery_array.ndim
3
mystery_array.shape
(3, 2, 4)
mystery_array[2, 1, 3]
18
mystery_array[2, 1]
array([97, 0, 27, 18])
mystery_array[:, :, 0]
array([[ 0, 4],
[ 7, 5],
[ 5, 97]])
a = np.arange(10,30)
print(a)
[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29]
a to:¶aa containing all the values except for the first 12 (i.e., [22, 23, 24, 25, 26, 27, 28, 29])a[-3:]
array([27, 28, 29])
a[3:6]
array([13, 14, 15])
a[12:]
array([22, 23, 24, 25, 26, 27, 28, 29])
a[::2]
array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28])
a, so that the first element comes last:¶[29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10]
If you need a hint, you can check out this part of the NumPy beginner's guide
np.flip(a)
array([29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13,
12, 11, 10])
b = [6,0,9,0,0,5,0]
np.nonzero(b)
(array([0, 2, 5]),)
Hint: Use the .random() function
from numpy.random import random
random((3,3,3))
array([[[0.40985817, 0.45258454, 0.88497001],
[0.02560356, 0.2239888 , 0.98561234],
[0.81811221, 0.02305315, 0.33259366]],
[[0.44806376, 0.05391684, 0.29878516],
[0.04332292, 0.80126497, 0.24534011],
[0.30860801, 0.90281948, 0.41889011]],
[[0.26148218, 0.69495833, 0.91607231],
[0.15823933, 0.36314199, 0.66732195],
[0.5950032 , 0.83777732, 0.62619449]]])
.linspace() to create a vector x of size 9 with values spaced out evenly between 0 to 100 (both included).¶x = np.linspace(0, 100, num=9)
x
array([ 0. , 12.5, 25. , 37.5, 50. , 62.5, 75. , 87.5, 100. ])
.linspace() to create another vector y of size 9 with values between -3 to 3 (both included). Then plot x and y on a line chart using Matplotlib.¶y = np.linspace(-3, 3, 9)
y
array([-3. , -2.25, -1.5 , -0.75, 0. , 0.75, 1.5 , 2.25, 3. ])
plt.plot(x,y)
plt.show
<function matplotlib.pyplot.show(close=None, block=None)>
noise = np.random.random((128, 128, 3))
plt.imshow(noise)
<matplotlib.image.AxesImage at 0x7ff8f40090c0>
v1 = np.array([4, 5, 2, 7])
v2 = np.array([2, 1, 3, 3])
v1 + v2
array([ 6, 6, 5, 10])
v1 * v2
array([ 8, 5, 6, 21])
# Python Lists vs ndarrays
list1 = [4, 5, 2, 7]
list2 = [2, 1, 3, 3]
list1 + list2
[4, 5, 2, 7, 2, 1, 3, 3]
list1 * list2
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[91], line 1 ----> 1 list1 * list2 TypeError: can't multiply sequence by non-int of type 'list'
array_2d = np.array([[1, 2, 3, 4],
[5, 6, 7, 8]])
array_2d + 6
array([[ 7, 8, 9, 10],
[11, 12, 13, 14]])
array_2d * 6
array([[ 6, 12, 18, 24],
[30, 36, 42, 48]])
a1 = np.array([[1, 3],
[0, 1],
[6, 2],
[9, 7]])
b1 = np.array([[4, 1, 3],
[5, 8, 5]])
print(f'{a1.shape}: a has {a1.shape[0]} rows and {a1.shape[1]} columns.')
print(f'{b1.shape}: b has {b1.shape[0]} rows and {b1.shape[1]} columns.')
print('Dimensions of result: (4x2)*(2x3)=(4x3)')
(4, 2): a has 4 rows and 2 columns. (2, 3): b has 2 rows and 3 columns. Dimensions of result: (4x2)*(2x3)=(4x3)
Challenge: Let's multiply a1 with b1. Looking at the wikipedia example above, work out the values for c12 and c33 on paper. Then use the .matmul() function or the @ operator to check your work.
np.matmul(a1, b1)
array([[19, 25, 18],
[ 5, 8, 5],
[34, 22, 28],
[71, 65, 62]])
a1 @ b1
array([[19, 25, 18],
[ 5, 8, 5],
[34, 22, 28],
[71, 65, 62]])
img = misc.face()
plt.imshow(img)
/tmp/ipykernel_113772/1403994607.py:1: DeprecationWarning: scipy.misc.face has been deprecated in SciPy v1.10.0; and will be completely removed in SciPy v1.12.0. Dataset methods have moved into the scipy.datasets module. Use scipy.datasets.face instead. img = misc.face()
<matplotlib.image.AxesImage at 0x7ff8f8985f60>
Challenge: What is the data type of img? Also, what is the shape of img and how many dimensions does it have? What is the resolution of the image?
print(f"Datatype: {type(img)}\nShape: {img.shape}(first 2 are resolution)\nDimensions: {img.ndim}")
Datatype: <class 'numpy.ndarray'> Shape: (768, 1024, 3)(first 2 are resolution) Dimensions: 3
Challenge: Convert the image to black and white. The values in our img range from 0 to 255.
grey_vals to convert the image to grey scale..imshow() together with the colormap parameter set to gray cmap=gray to look at the results.grey_vals = np.array([0.2126, 0.7152, 0.0722])
sRGB_array = img / 255
grey_scale = np.matmul(sRGB_array, grey_vals)
plt.imshow(grey_scale, cmap='gray')
<matplotlib.image.AxesImage at 0x7ff8d1d28700>
Challenge: Can you manipulate the images by doing some operations on the underlying ndarrays? See if you can change the values in the ndarray so that:
plt.imshow(np.flip(grey_scale), cmap='gray')
<matplotlib.image.AxesImage at 0x7ff8d1d2abf0>
plt.imshow(np.rot90(img))
<matplotlib.image.AxesImage at 0x7ff8d1ae9480>
plt.imshow(255 - img)
<matplotlib.image.AxesImage at 0x7ff8d0311240>
file_name = 'yummy_macarons.jpg'
wall = 'wall.jpg'
my_img = Image.open(file_name)
img_array = np.array(my_img)
plt.imshow(img_arrayp
<matplotlib.image.AxesImage at 0x7ff8d1d4b4c0>
plt.imshow(255 - img_array)
<matplotlib.image.AxesImage at 0x7ff8d1d1f430>
wall_img = Image.open(wall)
wall_array = np.array(wall_img)
plt.imshow(wall_array)
<matplotlib.image.AxesImage at 0x7ff8d02eee60>
plt.imshow(255 - wall_array)
<matplotlib.image.AxesImage at 0x7ff8cfed04f0>
grey_wall = np.matmul(wall_array/255, grey_vals)
plt.imshow(grey_wall)
<matplotlib.image.AxesImage at 0x7ff8cf8de590>
plt.imshow(255 - grey_wall)
<matplotlib.image.AxesImage at 0x7ff8cf79c460>
plt.imshow(grey_wall, cmap='gray')
<matplotlib.image.AxesImage at 0x7ff8cf769510>